//
//  BaseTableViewController.swift
//  NetProject
//
//  Created by EDY on 2021/11/1.
//

import Foundation
import UIKit

class BaseTableViewController: BaseViewController {
    var dataSource = [CommonModel]()
    var page = 1
    
    lazy var tableView : UITableView = {
        let myTableView = UITableView.init(frame: CGRect(x: 0, y: KNavAndStatusHeight, width: kCNScreenW, height: kCNScreenH - KNavAndStatusHeight ))
        myTableView.delegate = self
        myTableView.dataSource = self
        myTableView.tableFooterView = UIView.init()
        
        if #available (iOS 11.0,*) {
            myTableView.contentInsetAdjustmentBehavior = .never
        }else{
            self.automaticallyAdjustsScrollViewInsets = false
        }
        
        
        return myTableView
    }()
    
    lazy var footer : MJRefreshAutoNormalFooter = {
        let myFooter = MJRefreshAutoNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        
        return myFooter!
        
    }()
    
    lazy var header : MJRefreshNormalHeader = {
        let myHeader = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(updateData))
        
        return myHeader!
        
    }()
    
    func isHasFooterView(_ isTrue:Bool) -> Void {
        if isTrue {
            self.tableView.mj_footer = self.footer
        }else{
            self.tableView.mj_footer = nil
        }
    }
    
    
    func isHasHeaderView(_ isTrue:Bool) -> Void {
        if isTrue {
            self.tableView.mj_header = self.header
        }else{
            self.tableView.mj_header = nil
        }
    }
    
    // 上拉加载更多
    @objc func loadMoreData() -> Void{
        self.page += 1
    }
    
    // 下拉刷新
    @objc func updateData() -> Void{
        self.page = 1
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(self.tableView)
        
    }
        
}

extension BaseTableViewController : UITableViewDelegate{
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if (cell == nil) {
            cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
        }
//        cell?.textLabel?.text = self.dataSource[indexPath.row].cellTitle
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let model = self.dataSource[indexPath.row]
        if (model.cellContent.count != 0) {
//            let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
//            let classType:AnyClass? = NSClassFromString(nameSpace + "." + model.cellContent)
//            let viewController = classType as! UIViewController.Type
//            let vc = viewController.init()
//            vc.title = model.cellTitle
//            self.navigationController?.pushViewController(vc , animated: true)
        }
    }
}

extension BaseTableViewController : UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return Size(50)
    }
}
